let http = require('../../../utils/api.js');
let domain = require('../../../utils/api.js');
import {
    toData,
    myHttps
} from '../../../utils/util.js';
Page({

    data: {
        hiddenLoading: false,
        bulid: false, //已创建弹窗
        buliding: false, //创建中弹窗
        // 审核中弹窗
        audit: false,
        err: false,
        id: 'Complete', //默认选中的
        themeId: '', //主题id
        click_id: '',
        winHeight: "", //窗口高度
        currentTab: 0, //预设当前项的值
        scrollLeft: 0, //tab标题的滚动条位置
        listclick: [{
                'name': '已创建',
                'id': 'Complete'
            },
            {
                'name': '创建中',
                'id': 'Pending'
            }
        ],
        items: [], //主题列表数据
        userId: '', //用户id
        title: '', //分享页面需要得title
        content: '',
        themeid: '',
        imgs: '../../../images/bgbg.png',
        tab: 0,
        size: 10,
        page: 1,
        clientHeight:'',
        startX: 0, //开始坐标
        startY: 0,
        domain:'',//ip
        userInfo:'',//个人信息
    },

    onLoad: function(e) {
        var that = this;
        //  高度自适应
        wx.getSystemInfo({
            success: function (res) {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR - 180;
                // console.log(calc)
                that.setData({
                    winHeight: calc
                });
            }
        });
        var that = this
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    clientHeight: res.windowHeight
                });
            }
        });
        this.setData({
            userId: e.userId,
            domain:domain.domain
        })
        this.myfollow(this.data.userId, 'Complete', this.data.page, this.data.size); //进来默认显示已创建的主题
        this.GetInfo(); //用户详情
    },
    // 滚动切换标签样式
    switchTab: function (e) {
        this.setData({
            currentTab: e.detail.current
        });
        this.checkCor();
    },
    // 点击标题切换当前页时改变样式
    swichNav: function (e) {
        this.setData({
            items:[]
        })
        var cur = e.target.dataset.current;
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
        if (cur == 0) {
            this.myfollow(this.data.userId, 'Complete', 1, 10)
        }else if (cur == 1) {
            this.myfollow(this.data.userId, 'New', 1, 10)
        }
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function () {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    // 我的主题 接口
    myfollow(id, a, page, size,xiala) {
        let url = http.api.Mytheme + id + '/topics?status=' + a + '&page=' + page + '&size=' + size;
        var that = this;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            //   console.log(res)
            if (res.code == 1) {
                if (xiala) {
                    for (var i = 0; i < res.data.content.length; i++) {
                        res.data.content[i].insert_time = toData(res.data.content[i].insert_time, 1)
                        this.data.items = res.data.content
                    }
                }else {
                    for (var i = 0; i < res.data.content.length; i++) {
                        res.data.content[i].insert_time = toData(res.data.content[i].insert_time,1)
                        this.data.items.push(res.data.content[i])
                    }
                }
                that.setData({
                    items: this.data.items,
                    hiddenLoading: true
                });
                // console.log(this.data.items)
            }
        }).catch(err => {})
    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function() {
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            size: 10,
            page: 1,
            hiddenLoading: false
        })
        if (this.data.currentTab == 0) {
            this.myfollow(this.data.userId, 'Complete', this.data.page, this.data.size,'xiala')
        } else if (this.data.currentTab == 1) {
            this.myfollow(this.data.userId, 'New', this.data.page, this.data.size,'xiala')
        }
    },
    // 上拉加载
    onReachBottom: function(event) {
        this.setData({
            size: 10,
            page: this.data.page + 1,
            hiddenLoading: false
        })
        if (this.data.currentTab == 0) {
            this.myfollow(this.data.userId, 'Complete', this.data.page, this.data.size)
        } else if (this.data.currentTab == 1) {
            this.myfollow(this.data.userId, 'New', this.data.page, this.data.size)
        }
    },
    //点击每一项 跳转到主题主页
    gothemeHome(e) {
        // console.log(e)
        var status = e.currentTarget.dataset.status
        var id = e.currentTarget.dataset.id
        if (status == 'Complete') {
            wx.navigateTo({
                url: '../../themeHome/themeHome?id=' + id,
            })
        } else if (status == 'New'){
            wx.setStorageSync('themeid', id)
            //清除之前得主题缓存数据
            wx.removeStorage({
                key: 'apply',
            })
            wx.removeStorage({
                key: 'title',
            })
            wx.removeStorage({
                key: 'wgList',
            })
            wx.removeStorage({
                key: 'mediaData',
            })
            wx.removeStorage({
                key: 'avatar',
            })
            wx.redirectTo({
                url: '../../EditorTheme/EditorTheme',
            })
        }
    },
    // ... 弹窗 
    showss(e) {
        // console.log(e)
        this.setData({
            title: e.currentTarget.dataset.title,
            content: e.currentTarget.dataset.content,
            themeid: e.currentTarget.dataset.id
        })
        // 主题id
        let id = e.currentTarget.dataset.id
        wx.setStorageSync('themeids', id)
        // console.log(wx.getStorageSync('themeids'))
        let status = e.currentTarget.dataset.status
        // console.log(status)
        if (status == 'Pending') {
            this.setData({
                buliding: true,
                bulid: false,
                status: status,
                themeId: id
            })
        } else if (status == 'Approved') {
            this.setData({
                bulid: true,
                buliding: false,
                status: status,
                themeId: id
            })
        }
        // console.log(this.data.themeId)
    },
    // 弹窗取消的点击事件
    cancel() {
        this.setData({
            bulid: false,
            buliding: false
        });
    },
    // go编辑主题
    goEditorTheme(e) {
        // console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        wx.setStorageSync('themeid', id)
        this.cancel()
        //清除之前得主题缓存数据
        wx.removeStorage({
            key: 'apply',
        })
        wx.removeStorage({
            key: 'title',
        })
        wx.removeStorage({
            key: 'wgList',
        })
        wx.removeStorage({
            key: 'mediaData',
        })
        wx.removeStorage({
            key: 'avatar',
        })
        wx.redirectTo({
            url: '../../EditorTheme/EditorTheme',
        })
    },
    // 跳转留言审核 
    gomessage() {
        wx.navigateTo({
            url: '../follow/follow?id=' + this.data.themeId
        })
        this.cancel()
    },
    //手指触摸动作开始 记录起点X坐标

    touchstart: function (e) {
        // console.log(this.data.items)

        //开始触摸时 重置所有删除

        this.data.items.forEach(function (v, i) {

            if (v.isTouchMove) //只操作为true的

                v.isTouchMove = false;

        })

        this.setData({

            startX: e.changedTouches[0].clientX,

            startY: e.changedTouches[0].clientY,

            items: this.data.items

        })

    },

    //滑动事件处理

    touchmove: function (e) {

        var that = this,

            index = e.currentTarget.dataset.index, //当前索引

            startX = that.data.startX, //开始X坐标

            startY = that.data.startY, //开始Y坐标

            touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标

            touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标

            //获取滑动角度

            angle = that.angle({
                X: startX,
                Y: startY
            }, {
                    X: touchMoveX,
                    Y: touchMoveY
                });

        that.data.items.forEach(function (v, i) {

            v.isTouchMove = false

            //滑动超过30度角 return

            if (Math.abs(angle) > 30) return;

            if (i == index) {

                if (touchMoveX > startX) //右滑

                    v.isTouchMove = false

                else //左滑

                    v.isTouchMove = true

            }

        })

        //更新数据

        that.setData({

            items: that.data.items

        })

    },

    /**
  
    * 计算滑动角度
  
    * @param {Object} start 起点坐标
  
    * @param {Object} end 终点坐标
  
    */

    angle: function (start, end) {

        var _X = end.X - start.X,

            _Y = end.Y - start.Y

        //返回角度 /Math.atan()返回数字的反正切值

        return 360 * Math.atan(_Y / _X) / (2 * Math.PI);

    },

    //删除主题接口
    del: function (e) {
        this.setData({
            hiddenLoading: false
        })
        // console.log(e.currentTarget.dataset.id)
        let id = e.currentTarget.dataset.id
        let url = http.api.delMytheme + this.data.userId +'/topic/' + id ;
        myHttps({
            url: url,
            method: "DELETE",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    hiddenLoading: true
                })
                // this.myfollow(this.data.userId, 'Complete', 1, 10)
                this.data.items.splice(e.currentTarget.dataset.index, 1)
                this.setData({
                    items: this.data.items
                })
            }
        }).catch(err => { })
    },
    // 获取用户信息接口 
    GetInfo() {
        var url = http.api.userInfo + this.data.userId;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    userInfo: res.data
                })
            }
        }).catch(err => { })
    },
})